package com.mmy.controller.hotel;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mmy.domain.constzone.TConstZone;
import com.mmy.domain.hotel.THotelOrder;
import com.mmy.domain.hotel.THotelRoom;
import com.mmy.domain.hotel.VHotelBase;
import com.mmy.domain.hotel.VHotelPlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.constzone.ConstZoneServiceImpl;
import com.mmy.service.impl.hotel.THotelOrderServiceImpl;
import com.mmy.service.impl.hotel.THotelRoomServiceImpl;
import com.mmy.service.impl.hotel.VHotelBaseServiceImpl;
import com.mmy.service.impl.hotel.VHotelPlanServiceImpl;

@Controller
public class VHotelBaseController {
	@Autowired
	VHotelBaseServiceImpl vHotelBaseServiceImpl;
	@Autowired
	VHotelPlanServiceImpl vHotelPlanServiceImpl;
	@Autowired
	ConstZoneServiceImpl constZoneServiceImpl;
	@Autowired
	THotelRoomServiceImpl tHotelRoomServiceImpl;

	@Autowired
	THotelOrderServiceImpl tHotelOrderServiceImpl;

	
	/* 查询酒店信息 */
	@RequestMapping(value = "/searchhotelbase")
	public String FindHotelBase(HttpServletRequest request, Model model) {

		String strHotelName = request.getParameter("HotelName");
		String strZoneID = request.getParameter("ZoneID");
		String strRate = request.getParameter("Rate");
		String strType = request.getParameter("Type");
		List<VHotelBase> lstHotelBase = new ArrayList<VHotelBase>();
		if (strHotelName == "" && strZoneID == "" && strRate == "" && strType == "") {
			lstHotelBase = vHotelBaseServiceImpl.findALL();

		} else {
			lstHotelBase = vHotelBaseServiceImpl.findBySearch(strHotelName, strZoneID, strRate, strType);

		}
		model.addAttribute("newVHotelBase", lstHotelBase);

		/* 查询所有区域 国家或者省市区 */
		List<TConstZone> db_TConstZoneProvince = constZoneServiceImpl.findAllProvince();
		model.addAttribute("newTConstZoneProvince", db_TConstZoneProvince);
		List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
		model.addAttribute("newTConstZone", db_TConstZone);
		List<TConstZone> db_TConstZoneDistrict = constZoneServiceImpl.findAllDistrict();
		model.addAttribute("newTConstZoneDistrict", db_TConstZoneDistrict);
		request.setAttribute("strHotelName", strHotelName);
		request.setAttribute("strRate", strRate);
		return "webbackground/hotelmanagement/hotelindex";

	}

	/* 进入酒店详情信息 */
	@RequestMapping(value = "/hotelinfo")
	public String ToHotelBaseInfo(HttpServletRequest request, Model model) {

		int iHotelID = Integer.parseInt(request.getParameter("iHotelID"));

		VHotelBase vHotelBase = vHotelBaseServiceImpl.findByHotelID(iHotelID);
		model.addAttribute("newVHotelBase", vHotelBase);
		List<THotelRoom> tHotelRoom = tHotelRoomServiceImpl.findByHotelID(iHotelID);
		model.addAttribute("newTHotelRoom", tHotelRoom);
		return "webbackground/hotelmanagement/hotelinfo";
	}

	@RequestMapping(value = "/hotelorder")
	public String ToHotelOrder(HttpServletRequest request, Model model) {

		int iRoomID = Integer.parseInt(request.getParameter("iRoomID"));
		model.addAttribute("iRoomID", iRoomID);
		THotelRoom tHotelRoom = tHotelRoomServiceImpl.findByiRoomID(iRoomID);
		model.addAttribute("tHotelRoom", tHotelRoom);

		List<THotelOrder> lstHotelOrder = tHotelOrderServiceImpl.findByRoomID(iRoomID);
		model.addAttribute("orderlist", lstHotelOrder);

		return "webbackground/hotelmanagement/hotelorder";
	}

	@RequestMapping(value = "/orderhotelinfo")
	public String ToOrderHotelInfo(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		THotelOrder tHotelOrder = tHotelOrderServiceImpl.findByID(iID);
		model.addAttribute("tHotelOrder", tHotelOrder);

		String strOrderID = tHotelOrder.getOrderID();
		int iPlanID = Integer.parseInt(strOrderID.substring(9, 18));

		VHotelPlan vHotelPlan = vHotelPlanServiceImpl.findByPlanID(iPlanID);

		model.addAttribute("vHotelPlan", vHotelPlan);

		return "webbackground/hotelmanagement/orderhotelinfo";
	}

//收款打印页面
	@RequestMapping(value = "/printhotel")
	public String ToPrintHotel(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		THotelOrder tHotelOrder = tHotelOrderServiceImpl.findByID(iID);
		model.addAttribute("tHotelOrder", tHotelOrder);

		return "webbackground/hotelmanagement/printhotel";
	}

	// 退款打印页面
	@RequestMapping(value = "/printcancelhotel")
	public String ToPrintCancelHotel(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		THotelOrder tHotelOrder = tHotelOrderServiceImpl.findByID(iID);
		model.addAttribute("tHotelOrder", tHotelOrder);

		return "webbackground/hotelmanagement/printcancelhotel";
	}

	/**
	 * 函数功能说明： 出纳审核
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/cashierhotel")
	public String ToCashierHotel(Model model) {
		try {

			List<VHotelBase> lstHotelBase = vHotelBaseServiceImpl.findAll();
			model.addAttribute("newVHotelBase", lstHotelBase);

			return "OA/cashier/cashierhotel";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/cashierhotelroom")
	public String ToCashierHotelRoom(HttpServletRequest request, Model model) {
		try {

			int iHotelID = Integer.parseInt(request.getParameter("iHotelID"));

			List<THotelRoom> tHotelRoom = tHotelRoomServiceImpl.findByHotelID(iHotelID);
			model.addAttribute("newTHotelRoom", tHotelRoom);

			return "OA/cashier/cashierhotelroom";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/cashierhotelorder")
	public String ToCashierHotelOrder(HttpServletRequest request, Model model) {

		int iRoomID = Integer.parseInt(request.getParameter("iRoomID"));

		List<THotelOrder> lstHotelOrder = tHotelOrderServiceImpl.findByRoomID(iRoomID);
		model.addAttribute("orderlist", lstHotelOrder);

		model.addAttribute("iRoomID", iRoomID);

		return "OA/cashier/cashierhotelorder";
	}

	@RequestMapping(value = "/entercheckcashierhotel")
	public String EnterCheckCashierHotel(HttpServletRequest request, HttpSession session,
			String checkList) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		String[] checklist = checkList.split(",");
		List<Integer> lstCashierID = new ArrayList<>();
		List<THotelOrder> lstCashier = new ArrayList<>();

		for (String str : checklist) {
			lstCashierID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			THotelOrder tHotelOrder = tHotelOrderServiceImpl.findByID(i);

			if (tHotelOrder.getCashierState() == 0) {
				tHotelOrder.setCashierState((byte) 1);
			} else if (tHotelOrder.getCashierState() == 1) {
				tHotelOrder.setCashierState((byte) 2);
			} else {
				tHotelOrder.setCashierState((byte) 1);
			}
			tHotelOrder.setCashierDate(new Date());
			tHotelOrder.setCashierID(tSysUser.getUserID());

			lstCashier.add(tHotelOrder);
		}


		tHotelOrderServiceImpl.saveAll(lstCashier);
		return "OA/cashier/cashierhotelorder";

	}

//查询
	@RequestMapping(value = "/searchcashierhotel")
	public String FindCashierHotelOrder(HttpServletRequest request, Model model) {

		int iRoomID = Integer.parseInt(request.getParameter("iRoomID"));
		String strCashierState = request.getParameter("CashierState");
		if (strCashierState == "") {

			List<THotelOrder> lstHotelOrder = tHotelOrderServiceImpl.findByRoomID(iRoomID);
			model.addAttribute("orderlist", lstHotelOrder);
			model.addAttribute("iRoomID", iRoomID);
			return "OA/cashier/cashierhotelorder";
		} else {
			List<THotelOrder> lstHotelOrder = tHotelOrderServiceImpl.findByCashierAndRoomID(strCashierState, iRoomID);
			model.addAttribute("orderlist", lstHotelOrder);
			model.addAttribute("iRoomID", iRoomID);
			return "OA/cashier/cashierhotelorder";
		}

	}

	/**
	 * 函数功能说明：会计对账
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/accounthotel")
	public String ToAccountHotel(HttpServletRequest request, Model model) {

		List<VHotelBase> lstHotelBase = vHotelBaseServiceImpl.findAll();
		model.addAttribute("newVHotelBase", lstHotelBase);

		return "OA/account/accounthotel";
	}

	@RequestMapping(value = "/accounthotelroom")
	public String ToAccountHotelRoom(HttpServletRequest request, Model model) {
		try {

			int iHotelID = Integer.parseInt(request.getParameter("iHotelID"));

			List<THotelRoom> tHotelRoom = tHotelRoomServiceImpl.findByHotelID(iHotelID);
			model.addAttribute("newTHotelRoom", tHotelRoom);

			return "OA/account/accounthotelroom";
		} catch (Exception e) {

		}
		return null;

	}

	@RequestMapping(value = "/accounthotelorder")
	public String ToAccountHotelOrder(HttpServletRequest request, Model model) {

		int iRoomID = Integer.parseInt(request.getParameter("iRoomID"));

		List<THotelOrder> lstHotelOrder = tHotelOrderServiceImpl.findByRoomID(iRoomID);
		model.addAttribute("orderlist", lstHotelOrder);

		model.addAttribute("iRoomID", iRoomID);

		return "OA/account/accounthotelorder";
	}

	@RequestMapping(value = "/entercheckaccounthotel")
	public String EnterCheckAccountHotel(HttpServletRequest request, String checkList, HttpSession session) {

		TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");

		String[] checklist = checkList.split(",");
		List<Integer> lstAccountID = new ArrayList<>();
		List<THotelOrder> lstAccount = new ArrayList<>();

		for (String str : checklist) {
			lstAccountID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			THotelOrder tHotelOrder = tHotelOrderServiceImpl.findByID(i);

			if (tHotelOrder.getAccountState() == 0) {
				tHotelOrder.setAccountState((byte) 1);
			} else if (tHotelOrder.getAccountState() == 1) {
				tHotelOrder.setAccountState((byte) 2);
			} else {
				tHotelOrder.setAccountState((byte) 1);
			}
			tHotelOrder.setAccountDate(new Date());
			tHotelOrder.setAccountID(tSysUser.getUserID());

			lstAccount.add(tHotelOrder);
		}

		tHotelOrderServiceImpl.saveAll(lstAccount);

		return "OA/account/accounthotelorder";

	}

//	结账汇款
	@RequestMapping(value = "/entercheckremittancehotel")
	public String EnterCheckRemittanceHotel(HttpServletRequest request, String checkList) {

		String[] checklist = checkList.split(",");
		List<Integer> lstRemittanceID = new ArrayList<>();
		List<THotelOrder> lstRemittance = new ArrayList<>();

		for (String str : checklist) {
			lstRemittanceID.add(Integer.parseInt(str));
			int i = Integer.parseInt(str);
			THotelOrder tHotelOrder = tHotelOrderServiceImpl.findByID(i);

			if (tHotelOrder.getRemittanceState() == 0) {
				tHotelOrder.setRemittanceState((byte) 1);
			} else {
				tHotelOrder.setRemittanceState((byte) 0);
			}

			lstRemittance.add(tHotelOrder);
		}

		tHotelOrderServiceImpl.saveAll(lstRemittance);

		return "OA/account/accounthotelorder";

	}

//查询
	@RequestMapping(value = "/searchaccounthotel")
	public String FindAccountHotel(HttpServletRequest request, Model model) {

		int iRoomID = Integer.parseInt(request.getParameter("iRoomID"));
		String strAccountState = request.getParameter("AccountState");

		if (strAccountState == "") {
			List<THotelOrder> lstHotelOrder = tHotelOrderServiceImpl.findByRoomID(iRoomID);
			model.addAttribute("orderlist", lstHotelOrder);
			model.addAttribute("iRoomID", iRoomID);

			return "OA/account/accounthotelorder";
		} else {
			List<THotelOrder> lstHotelOrder = tHotelOrderServiceImpl.findByAccountAndRoomID(strAccountState, iRoomID);
			model.addAttribute("orderlist", lstHotelOrder);
			model.addAttribute("iRoomID", iRoomID);

			return "OA/account/accounthotelorder";
		}

	}

	/* 前台进入酒店页面 */
	@RequestMapping(value = "/qthotelindex")
	public String qtHotelIndex(HttpServletRequest request, Model mHotelBase) {
		/* 查询所有区域 市 */
		List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
		mHotelBase.addAttribute("newTConstZone", db_TConstZone);
		/* 查询所有酒店 */
		List<VHotelBase> db_VHotelBase = vHotelBaseServiceImpl.findByZoneID(db_TConstZone.get(0).getZoneID());
		mHotelBase.addAttribute("newVHotelBase", db_VHotelBase);
		/* 查询所有房间 */
		List<THotelRoom> db_THotelRoom = tHotelRoomServiceImpl.findALL();
		mHotelBase.addAttribute("newTHotelRoom", db_THotelRoom);
		/* 查询所有最小价格的酒店计划 */
		List<VHotelPlan> lstHotelPlan = vHotelPlanServiceImpl.findAllMin();
		mHotelBase.addAttribute("newHotelPlanList", lstHotelPlan);
		return "hotel/hotelindex";
	}

	/* 前台查询 */

	@RequestMapping(value = "/qtsearchhotel")
	public String qtSearchHotel(HttpServletRequest request, Model model) {

		String strHotelName = request.getParameter("HotelName");
		String strZoneID = request.getParameter("ZoneID");
		String strType = request.getParameter("Type");
		List<VHotelBase> lstHotelBase = new ArrayList<VHotelBase>();
		if (strHotelName == "" && strZoneID != "" && strType == "") {
			lstHotelBase = vHotelBaseServiceImpl.findByZoneID(Integer.parseInt(strZoneID));

		} else {
			lstHotelBase = vHotelBaseServiceImpl.findBySearchHotel(strHotelName, strZoneID, strType);

		}
		model.addAttribute("newVHotelBase", lstHotelBase);

		/* 查询所有区域 国家或者省市区 */

		List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
		model.addAttribute("newTConstZone", db_TConstZone);
		/* 查询所有房间 */
		List<THotelRoom> db_THotelRoom = tHotelRoomServiceImpl.findALL();
		model.addAttribute("newTHotelRoom", db_THotelRoom);
		/* 查询所有最小价格的酒店计划 */
		List<VHotelPlan> lstHotelPlan = vHotelPlanServiceImpl.findAllMin();
		model.addAttribute("newHotelPlanList", lstHotelPlan);
		request.setAttribute("strHotelName", strHotelName);

		return "hotel/hotelindex";

	}

	/* 前台点击进入详情页 */
	/* 进入酒店详情信息 */
	@RequestMapping(value = "/hoteldetail")
	public String ToHotelDetail(HttpServletRequest request, Model model) {

		int iHotelID = Integer.parseInt(request.getParameter("HotelID"));

		VHotelBase vHotelBase = vHotelBaseServiceImpl.findByHotelID(iHotelID);
		model.addAttribute("newVHotelBase", vHotelBase);
		/*
		 * 查询该酒店的房间名称并且去重 List<String> lstRoomName =
		 * tHotelRoomServiceImpl.getNameByHotelID(iHotelID);
		 * model.addAttribute("newLstRoomName", lstRoomName);
		 */
		/* 查询所有房间 */
		List<THotelRoom> db_THotelRoom = tHotelRoomServiceImpl.findALL();
		model.addAttribute("newTHotelRoom", db_THotelRoom);
		/* 查询房间的当前日期的计划 */
		List<VHotelPlan> lstHotelPlan = vHotelPlanServiceImpl.findAllNow();
		model.addAttribute("newHotelPlanList", lstHotelPlan);
		/* 查询所有区域 国家或者省市区 */

		List<TConstZone> db_TConstZone = constZoneServiceImpl.findAllCity();
		model.addAttribute("newTConstZone", db_TConstZone);

		return "hotel/hotelinfo";
	}
}
